package com.hzhc.controller.carteam;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.CarInfo;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.CarInfoService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.CarInfoVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/7/25 11:33
 * @description CarInfoController
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/carInfo")
public class CarInfoController {

    private  final CarInfoService carInfoService;

    /**
     * 车牌格式
     * @param request
     * @return
     */
    @RequestMapping("/getCarNo")
    public ResultUtil<T> getOrganzationTree(HttpServletRequest request, @RequestBody CarInfo carInfo){
        return carInfoService.carNoCheck(carInfo);
    }

    //车辆黑名单校验
    @RequestMapping("/checkIsBlack")
    public ResultUtil<T> checkIsBlack(@RequestBody CarInfo carInfo){
        return carInfoService.checkIsBlack(carInfo);
    }

    /**
     * 车辆信息列表
     * @return
     */
    @RequestMapping("/getCarInfoList")
    public ResultUtil<List<CarInfo>> getCarInfoList(@RequestBody CarInfoVo vo){
        Page<CarInfo> page =new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<CarInfo> list =carInfoService.getList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    //YZS 车辆add
    @RequestMapping("/saveCarInfo")
    @SysTemLog(operModul = "车队管理-车辆添加", operType = "添加", operDesc = "车辆添加功能", modular = "PC")
    public ResultUtil<T> saveCarInfo(HttpServletRequest httpServletRequest,@RequestBody CarInfoVo carInfo) {
        UserDto user= TokenUtil.getUser(httpServletRequest);
        carInfo.setUserId(user.getId());
        CarInfo info = new CarInfo();
        BeanUtils.copyProperties(carInfo,info);
        info.setHdId(carInfo.getCompanyId());
        return carInfoService.addRet1(info);
    }

    //YZS 车辆deit
    @RequestMapping("/editCarInfo")
    @SysTemLog(operModul = "车队管理-车辆添加", operType = "编辑", operDesc = "车辆编辑功能", modular = "PC")
    public ResultUtil<T> editCarInfo(HttpServletRequest request,@RequestBody CarInfoVo carInfo){
        Long userID = TokenUtil.getUserID(request);
        carInfo.setUpdateTime(LocalDateTime.now());
        carInfo.setUpdateUserId(userID);
        CarInfo info = new CarInfo();
        BeanUtils.copyProperties(carInfo,info);
        info.setHdId(carInfo.getCompanyId());
        return carInfoService.updateRet(info);
    }

    //yzs 车辆delete
    @RequestMapping("/delCarInfo")
    @SysTemLog(operModul = "车队管理-车辆删除", operType = "删除", operDesc = "车辆删除功能", modular = "PC")
    public ResultUtil<T>  delCarInfo(@RequestBody CarInfoVo vo) {

        return carInfoService.deleteById(vo);
    }
    /**
     * 车辆导入
     * 保存上传的车辆
     */
    @PostMapping("/importYZSCarList")
    public ResultUtil<T> importCarList(HttpServletRequest request, @RequestBody CarInfoVo vo) {
        return carInfoService.importYZSCarList(request, vo.getCarList());
    }
    /**
     * 车辆清单导出模板下载
     */
    private final static String EXPORT_EXCEL_PATH_YZS = "static/download/车辆导入.xlsx";

    @GetMapping("/exportYZSCarFile")
    public void exportCarFile(HttpServletRequest request, HttpServletResponse response) {
        carInfoService.exportYzsCarFile(request, response, EXPORT_EXCEL_PATH_YZS);
    }

    /**
     *车辆导入
     */
    @RequestMapping("/carYZSUpload")
    public ResultUtil<List<CarInfoVo>> carUpload(MultipartFile file, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        return carInfoService.CarYzsUpload(user,file);
    }
}
